package com.pap.datas.dict.agent.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.tamper.BeanCopy;
import com.alibaba.tamper.process.convertor.Convertor;
import com.alibaba.tamper.process.convertor.ConvertorHelper;
import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.pap.base.service.impl.PapBaseServiceImpl;
import com.pap.base.util.date.DateUtils;
import com.pap.base.util.string.StringUtilss;
import com.pap.datas.agent.dict.IDictAgent;
import com.pap.datas.agent.dict.dto.DictDTO;
import com.pap.datas.agent.dict.dto.DictDetailDTO;
import com.pap.datas.dict.auto.entity.Dict;
import com.pap.datas.dict.auto.entity.DictDetail;
import com.pap.datas.dict.auto.mapper.DictDetailMapper;

@Transactional
@Service("dictAgent")
public class DictAgentImpl extends PapBaseServiceImpl<Dict> implements IDictAgent {

	@Autowired
	private DictDetailMapper dictDetailMapper;

	@Override
	public int saveOrUpdateDictWithDetailInfo(DictDTO operObj, String clientLicenseId, String loginUserId) {
		if(operObj != null) {
			// 如果当前传入字典编码不为空，则优先删除明细字典数据
			String inputDictId = operObj.getId();
			String currentOperDictId = inputDictId;
			if(StringUtilss.isNotEmpty(currentOperDictId)) {
				dictDetailMapper.deleteByDictId(currentOperDictId);
			} else {
				currentOperDictId = UUID.randomUUID().toString();
			}
			
			// BeanCopy
			Dict databaseDict = new Dict();
			BeanCopy.create(DictDTO.class, Dict.class).copy(operObj, databaseDict);
			
			List<DictDetail> databaseDictDetailList = new ArrayList<DictDetail>();
			List<DictDetailDTO> inputDetailList = operObj.getDetails();
			if(inputDetailList != null) {
				ConvertorHelper helper = new ConvertorHelper();
				Convertor intSet = helper.getConvertor(List.class, Set.class);
				databaseDictDetailList = (List) intSet.convertCollection(inputDetailList, List.class, DictDetail.class);
			}
			
			if(StringUtilss.isNotEmpty(inputDictId)) {
				mapper.updateByPrimaryKeySelective(databaseDict);
			} else {
				databaseDict.setId(currentOperDictId);
				if(StringUtilss.isEmpty(operObj.getCode())) {
					try {
						databaseDict.setCode(PinyinHelper.convertToPinyinString(databaseDict.getName(), "", PinyinFormat.WITHOUT_TONE));
					} catch (PinyinException e) {
						e.printStackTrace();
					}
				}
				databaseDict.setCreateIp("0.0.0.0");
				databaseDict.setCreateUser(loginUserId);
				databaseDict.setCreateTime(DateUtils.getCurrDateTimeStr());
				databaseDict.setClientLicenseId(clientLicenseId);
				mapper.insertSelective(databaseDict);
			}
			if(databaseDictDetailList != null) {
				for (DictDetail detailDTO : databaseDictDetailList) {
					detailDTO.setId(UUID.randomUUID().toString());
					detailDTO.setDictId(currentOperDictId);
					detailDTO.setCreateIp("0.0.0.0");
					detailDTO.setCreateUser(loginUserId);
					detailDTO.setCreateTime(DateUtils.getCurrDateTimeStr());
					detailDTO.setClientLicenseId(clientLicenseId);
					dictDetailMapper.insertSelective(detailDTO);
				}
			}
		}
		return 1;
	}

	@Override
	public int deleteDictWithDetailInfo(DictDTO operObj, String clientLicenseId, String loginUserId) {
		String inputDictId = operObj.getId();
		mapper.deleteByPrimaryKey(inputDictId);
		dictDetailMapper.deleteByDictId(inputDictId);
		return 0;
	}

	@Override
	public DictDTO selectDictWithDetailInfo(String inputSelectDictId, String clientLicenseId, String loginUserId) {
		DictDTO dictDTO = new DictDTO();
		List<DictDetailDTO> dictDetailDTOList = new ArrayList<DictDetailDTO>();

		Dict databaseDict = mapper.selectByPrimaryKey(inputSelectDictId);
		if(databaseDict != null) {
			BeanCopy.create(Dict.class, DictDTO.class).copy(databaseDict, dictDTO);
		}
		
		
		Map<Object, Object> operSelectDictMap = new HashMap<Object, Object>();
		operSelectDictMap.put("dictId", inputSelectDictId);
		List<DictDetail> databaseDictDetailList = dictDetailMapper.selectListByMap(operSelectDictMap);
		if(databaseDictDetailList != null) {
			ConvertorHelper helper = new ConvertorHelper();
			Convertor intSet = helper.getConvertor(List.class, Set.class);
			dictDetailDTOList = (List) intSet.convertCollection(databaseDictDetailList, List.class, DictDetailDTO.class);
		}
		
		dictDTO.setDetails(dictDetailDTOList);
		
		return dictDTO;
	}

	@Override
	public DictDTO selectDictWithDetailInfoByCode(String inputSelectDictCode, String clientLicenseId,
			String loginUserId) {
		DictDTO dictDTO = new DictDTO();
		List<DictDetailDTO> dictDetailDTOList = new ArrayList<DictDetailDTO>();

		Map<Object, Object> operSelectDictMap = new HashMap<Object, Object>();
		operSelectDictMap.put("code", inputSelectDictCode);
		operSelectDictMap.put("clientLicenseId", clientLicenseId);
		
		List<Dict> databaseDictList = mapper.selectListByMap(operSelectDictMap);
		if(databaseDictList != null && databaseDictList.size() > 0) {
			Dict databaseDict = databaseDictList.get(0);
			BeanCopy.create(Dict.class, DictDTO.class).copy(databaseDict, dictDTO);
		}
		
		
		Map<Object, Object> operSelectDictDetailMap = new HashMap<Object, Object>();
		operSelectDictDetailMap.put("dictId", dictDTO.getId());
		List<DictDetail> databaseDictDetailList = dictDetailMapper.selectListByMap(operSelectDictDetailMap);
		if(databaseDictDetailList != null) {
			ConvertorHelper helper = new ConvertorHelper();
			Convertor intSet = helper.getConvertor(List.class, Set.class);
			dictDetailDTOList = (List) intSet.convertCollection(databaseDictDetailList, List.class, DictDetailDTO.class);
		}
		
		dictDTO.setDetails(dictDetailDTOList);
		
		return dictDTO;
	}

}
